home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DOS.SWG / 0034_Share Multi-Tasking.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  10KB  |  431 lines

  1. {
  2. From: JON JASIUNAS
  3. Subj: Share Multi-tasking
  4. }
  5.  
  6. {**************************
  7.  *     SHARE.PAS v1.0     *
  8.  *                        *
  9.  *  General purpose file  *
  10.  *    sharing routines    *
  11.  **************************
  12.  
  13. 1992-93 HyperDrive Software
  14. Released into the public domain.}
  15.  
  16. {$S-,R-,D-}
  17. {$IFOPT O+}
  18.   {$F+}
  19. {$ENDIF}
  20.  
  21. unit Share;
  22.  
  23. {\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\}
  24.                                    interface
  25. {/////////////////////////////////////////////////////////////////////////////}
  26.  
  27. const
  28.   MaxLockRetries : Byte = 10;
  29.  
  30.   NormalMode = $02; { ---- 0010 }
  31.   ReadOnly   = $00; { ---- 0000 }
  32.   WriteOnly  = $01; { ---- 0001 }
  33.   ReadWrite  = $02; { ---- 0010 }
  34.   DenyAll    = $10; { 0001 ---- }
  35.   DenyWrite  = $20; { 0010 ---- }
  36.   DenyRead   = $30; { 0011 ---- }
  37.   DenyNone   = $40; { 0100 ---- }
  38.   NoInherit  = $70; { 1000 ---- }
  39.  
  40. type
  41.   Taskers = (NoTasker, DesqView, DoubleDOS, Windows, OS2, NetWare);
  42.  
  43. var
  44.   MultiTasking: Boolean;
  45.   MultiTasker : Taskers;
  46.   VideoSeg    : Word;
  47.   VideoOfs    : Word;
  48.  
  49. procedure SetFileMode(Mode: Word);
  50.   {- Set filemode for typed/untyped files }
  51.  
  52. procedure ResetFileMode;
  53.   {- Reset filemode to ReadWrite (02h) }
  54.  
  55. procedure LockFile(var F);
  56.   {- Lock file F }
  57.  
  58. procedure UnLockFile(var F);
  59.   {- Unlock file F }
  60.  
  61. procedure LockBytes(var F;  Start, Bytes: LongInt);
  62.   {- Lock Bytes bytes of file F, starting with Start }
  63.  
  64. procedure UnLockBytes(var F;  Start, Bytes: LongInt);
  65.   {- Unlock Bytes bytes of file F, starting with Start }
  66.  
  67. procedure LockRecords(var F;  Start, Records: LongInt);
  68.   {- Lock Records records of file F, starting with Start }
  69.  
  70. procedure UnLockRecords(var F;  Start, Records: LongInt);
  71.   {- Unlock Records records of file F, starting with Start }
  72.  
  73. function  TimeOut: Boolean;
  74.   {- Check for LockRetry timeout }
  75.  
  76. procedure TimeOutReset;
  77.   {- Reset internal LockRetry counter }
  78.  
  79. function  InDos: Boolean;
  80.   {- Is DOS busy? }
  81.  
  82. procedure GiveTimeSlice;
  83.   {- Give up remaining CPU time slice }
  84.  
  85. procedure BeginCrit;
  86.   {- Enter critical region }
  87.  
  88. procedure EndCrit;
  89.   {- End critical region }
  90.  
  91. {\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\}
  92.                                  implementation
  93. {/////////////////////////////////////////////////////////////////////////////}
  94.  
  95. uses
  96.   Dos;
  97.  
  98. var
  99.   InDosFlag: ^Word;
  100.   LockRetry: Byte;
  101.  
  102. {=============================================================================}
  103.  
  104. procedure FLock(Handle: Word; Pos, Len: LongInt);
  105. Inline(
  106.   $B8/$00/$5C/    {  mov   AX,$5C00        ;DOS FLOCK, Lock subfunction}
  107.   $8B/$5E/$04/    {  mov   BX,[BP + 04]    ;Place file handle in Bx register}
  108.   $C4/$56/$06/    {  les   DX,[BP + 06]    ;Load position in ES:DX}
  109.   $8C/$C1/        {  mov   CX,ES           ;Move ES pointer to CX register}
  110.   $C4/$7E/$08/    {  les   DI,[BP + 08]    ;Load length in ES:DI}
  111.   $8C/$C6/        {  mov   SI,ES           ;Move ES pointer to SI register}
  112.   $CD/$21);       {  int   $21             ;Call DOS}
  113.  
  114. {-----------------------------------------------------------------------------}
  115.  
  116. procedure FUnlock(Handle: Word; Pos, Len: LongInt);
  117. Inline(
  118.   $B8/$01/$5C/    {  mov   AX,$5C01        ;DOS FLOCK, Unlock subfunction}
  119.   $8B/$5E/$04/    {  mov   BX,[BP + 04]    ;Place file handle in Bx register}
  120.   $C4/$56/$06/    {  les   DX,[BP + 06]    ;Load position in ES:DX}
  121.   $8C/$C1/        {  mov   CX,ES           ;Move ES pointer to CX register}
  122.   $C4/$7E/$08/    {  les   DI,[BP + 08]    ;Load length in ES:DI}
  123.   $8C/$C6/        {  mov   SI,ES           ;Move ES pointer to SI register}
  124.   $CD/$21);       {  int   $21             ;Call DOS}
  125.  
  126. {=============================================================================}
  127.  
  128. procedure SetFileMode(Mode: Word);
  129. begin
  130.   FileMode := Mode;
  131. end;    { SetFileMode }
  132.  
  133. {-----------------------------------------------------------------------------}
  134.  
  135. procedure ResetFileMode;
  136. begin
  137.   FileMode := NormalMode;
  138. end;    { ResetFileMode }
  139.  
  140. {-----------------------------------------------------------------------------}
  141.  
  142. procedure LockFile(var F);
  143. begin
  144.   If not MultiTasking then
  145.     Exit;
  146.  
  147.   While InDos do
  148.     GiveTimeSlice;
  149.  
  150.   FLock(FileRec(F).Handle, 0, FileSize(File(F)));
  151. end;    { LockFile }
  152.  
  153. {-----------------------------------------------------------------------------}
  154.  
  155. procedure UnLockFile(var F);
  156. begin
  157.   If not MultiTasking then
  158.     Exit;
  159.  
  160.   While InDos do
  161.     GiveTimeSlice;
  162.  
  163.   FLock(FileRec(F).Handle, 0, FileSize(File(F)));
  164. end;    { UnLockFile }
  165.  
  166. {-----------------------------------------------------------------------------}
  167.  
  168. procedure LockBytes(var F;  Start, Bytes: LongInt);
  169. begin
  170.   If not MultiTasking then
  171.     Exit;
  172.  
  173.   While InDos do
  174.     GiveTimeSlice;
  175.  
  176.   FLock(FileRec(F).Handle, Start, Bytes);
  177. end;    { LockBytes }
  178.  
  179. {-----------------------------------------------------------------------------}
  180.  
  181. procedure UnLockBytes(var F;  Start, Bytes: LongInt);
  182. begin
  183.   If not MultiTasking then
  184.     Exit;
  185.  
  186.   While InDos do
  187.     GiveTimeSlice;
  188.  
  189.   FLock(FileRec(F).Handle, Start, Bytes);
  190. end;    { UnLockBytes }
  191.  
  192. {-----------------------------------------------------------------------------}
  193.  
  194. procedure LockRecords(var F;  Start, Records: LongInt);
  195. begin
  196.   If not MultiTasking then
  197.     Exit;
  198.  
  199.   While InDos do
  200.     GiveTimeSlice;
  201.  
  202.   FLock(FileRec(F).Handle, Start * FileRec(F).RecSize, Records * FileRec(F).Rec
  203. end;    { LockBytes }
  204.  
  205. {-----------------------------------------------------------------------------}
  206.  
  207. procedure UnLockRecords(var F;  Start, Records: LongInt);
  208. begin
  209.   If not MultiTasking then
  210.     Exit;
  211.  
  212.   While InDos do
  213.     GiveTimeSlice;
  214.  
  215.   FLock(FileRec(F).Handle, Start * FileRec(F).RecSize, Records * FileRec(F).Rec
  216. end;    { UnLockBytes }
  217.  
  218. {-----------------------------------------------------------------------------}
  219.  
  220. function  TimeOut: Boolean;
  221. begin
  222.   GiveTimeSlice;
  223.   TimeOut := True;
  224.  
  225.   If MultiTasking and (LockRetry < MaxLockRetries) then
  226.     begin
  227.       TimeOut := False;
  228.       Inc(LockRetry);
  229.     end;  { If }
  230. end;    { TimeOut }
  231.  
  232. {-----------------------------------------------------------------------------}
  233.  
  234. procedure TimeOutReset;
  235. begin
  236.   LockRetry := 0;
  237. end;    { TimeOutReset }
  238.  
  239. {-----------------------------------------------------------------------------}
  240.  
  241. function  InDos: Boolean;
  242. begin   { InDos }
  243.   InDos := InDosFlag^ > 0;
  244. end;    { InDos }
  245.  
  246. {-----------------------------------------------------------------------------}
  247.  
  248. procedure GiveTimeSlice;  ASSEMBLER;
  249. asm     { GiveTimeSlice }
  250.   cmp   MultiTasker, DesqView
  251.   je    @DVwait
  252.   cmp   MultiTasker, DoubleDOS
  253.   je    @DoubleDOSwait
  254.   cmp   MultiTasker, Windows
  255.   je    @WinOS2wait
  256.   cmp   MultiTasker, OS2
  257.   je    @WinOS2wait
  258.   cmp   MultiTasker, NetWare
  259.   je    @Netwarewait
  260.  
  261. @Doswait:
  262.   int   $28
  263.   jmp   @WaitDone
  264.  
  265. @DVwait:
  266.   mov   AX,$1000
  267.   int   $15
  268.   jmp   @WaitDone
  269.  
  270. @DoubleDOSwait:
  271.   mov   AX,$EE01
  272.   int   $21
  273.   jmp   @WaitDone
  274.  
  275. @WinOS2wait:
  276.   mov   AX,$1680
  277.   int   $2F
  278.   jmp   @WaitDone
  279.  
  280. @Netwarewait:
  281.   mov   BX,$000A
  282.   int   $7A
  283.   jmp   @WaitDone
  284.  
  285. @WaitDone:
  286. end;    { TimeSlice }
  287.  
  288. {----------------------------------------------------------------------------}
  289.  
  290. procedure BeginCrit;  ASSEMBLER;
  291. asm     { BeginCrit }
  292.   cmp   MultiTasker, DesqView
  293.   je    @DVCrit
  294.   cmp   MultiTasker, DoubleDOS
  295.   je    @DoubleDOSCrit
  296.   cmp   MultiTasker, Windows
  297.   je    @WinCrit
  298.   jmp   @EndCrit
  299.  
  300. @DVCrit:
  301.   mov   AX,$101B
  302.   int   $15
  303.   jmp   @EndCrit
  304.  
  305. @DoubleDOSCrit:
  306.   mov   AX,$EA00
  307.   int   $21
  308.   jmp   @EndCrit
  309.  
  310. @WinCrit:
  311.   mov   AX,$1681
  312.   int   $2F
  313.   jmp   @EndCrit
  314.  
  315. @EndCrit:
  316. end;    { BeginCrit }
  317.  
  318. {----------------------------------------------------------------------------}
  319.  
  320. procedure EndCrit;  ASSEMBLER;
  321. asm     { EndCrit }
  322.   cmp   MultiTasker, DesqView
  323.   je    @DVCrit
  324.   cmp   MultiTasker, DoubleDOS
  325.   je    @DoubleDOSCrit
  326.   cmp   MultiTasker, Windows
  327.   je    @WinCrit
  328.   jmp   @EndCrit
  329.  
  330. @DVCrit:
  331.   mov   AX,$101C
  332.   int   $15
  333.   jmp   @EndCrit
  334.  
  335. @DoubleDOSCrit:
  336.   mov   AX,$EB00
  337.   int   $21
  338.   jmp   @EndCrit
  339.  
  340. @WinCrit:
  341.   mov   AX,$1682
  342.   int   $2F
  343.   jmp   @EndCrit
  344.  
  345. @EndCrit:
  346. end;    { EndCrit }
  347.  
  348. {============================================================================}
  349.  
  350. begin { Share }
  351.   {- Init }
  352.   LockRetry:= 0;
  353.  
  354.   asm
  355.   @CheckDV:
  356.     mov   AX, $2B01
  357.     mov   CX, $4445
  358.     mov   DX, $5351
  359.     int   $21
  360.     cmp   AL, $FF
  361.     je    @CheckDoubleDOS
  362.     mov   MultiTasker, DesqView
  363.     jmp   @CheckDone
  364.  
  365.   @CheckDoubleDOS:
  366.     mov   AX, $E400
  367.     int   $21
  368.     cmp   AL, $00
  369.     je    @CheckWindows
  370.     mov   MultiTasker, DoubleDOS
  371.     jmp   @CheckDone
  372.  
  373.   @CheckWindows:
  374.     mov   AX, $1600
  375.     int   $2F
  376.     cmp   AL, $00
  377.     je    @CheckOS2
  378.     cmp   AL, $80
  379.     je    @CheckOS2
  380.     mov   MultiTasker, Windows
  381.     jmp   @CheckDone
  382.  
  383.   @CheckOS2:
  384.     mov   AX, $3001
  385.     int   $21
  386.     cmp   AL, $0A
  387.     je    @InOS2
  388.     cmp   AL, $14
  389.     jne   @CheckNetware
  390.   @InOS2:
  391.     mov   MultiTasker, OS2
  392.     jmp   @CheckDone
  393.  
  394.   @CheckNetware:
  395.     mov   AX,$7A00
  396.     int   $2F
  397.     cmp   AL,$FF
  398.     jne   @NoTasker
  399.     mov   MultiTasker, NetWare
  400.     jmp   @CheckDone
  401.  
  402.   @NoTasker:
  403.     mov   MultiTasker, NoTasker
  404.  
  405.   @CheckDone:
  406.   {-Set MultiTasking }
  407.     cmp   MultiTasker, NoTasker
  408.     mov   VideoSeg, $B800
  409.     mov   VideoOfs, $0000
  410.     je    @NoMultiTasker
  411.     mov   MultiTasking, $01
  412.   {-Get video address }
  413.     mov   AH, $FE
  414.     les   DI, [$B8000000]
  415.     int   $10
  416.     mov   VideoSeg, ES
  417.     mov   VideoOfs, DI
  418.     jmp   @Done
  419.  
  420.   @NoMultiTasker:
  421.     mov   MultiTasking, $00
  422.  
  423.   @Done:
  424.   {-Get InDos flag }
  425.     mov   AH, $34
  426.     int   $21
  427.     mov   WORD PTR InDosFlag, BX
  428.     mov   WORD PTR InDosFlag + 2, ES
  429.   end;  { asm }
  430. end.  { Share }
  431.